package compilador.util;

import java.io.File;
import java.io.IOException;

public class CriaExecutavel {

	private final String defaultPathDotNet = pathCombine("C:", "Windows", "Microsoft.NET");
	private final String[] frameworkFolders = { "Framework", "Framework64" };
	private final String[] frameworkVersions = { "v4.0.30319", "v3.5", "v3.0", "v2.0.50727", "v1.1.4322", "v1.0.3705" };
	
	public void buildCode(String caminhoIl) {
		String folder = "", subFolder = "";
		
		for (int i = 0; i < frameworkFolders.length; i++) {
			folder = pathCombine(defaultPathDotNet, frameworkFolders[i]);
			
			for (int j = 0; j < frameworkVersions.length; j++) {
				subFolder = pathCombine(folder, frameworkVersions[j]);
				
				if (new File(subFolder).exists()) { // verifica se existe a pasta
					if (new File(pathCombine(subFolder, "ilasm.exe")).exists()){ // verifica se existe o ilasm.exe
						try {
							Runtime.getRuntime().exec(pathCombine(subFolder, "ilasm.exe") + " " + caminhoIl);
						} catch (IOException e) {
							e.printStackTrace();
						}
						
						return;
					}
				}
			}
		}
	}
	
	private String pathCombine(Object ... args) {
		StringBuilder retorno = new StringBuilder();
		
		for (int i = 0; i < args.length; i++) {
			retorno.append(args[i] + File.separator);
		}
		
		try {
			return retorno.substring(0, retorno.length() - File.separator.length()).toString();	
		} catch (Exception e) {
			return "";
		}
	}
	
}
